using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
/// Summary description for InsertInput
/// </summary>
public class InsertInput
{
	public InsertInput()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public ArrayList InsertOperation(DataSet dsePageInfo, DataSet dseInputInfo)
    {
        string strPageId = "";
        string sAF_ROW_ID = "";
        ArrayList arlCmdObj = new ArrayList();
        ArrayList arlPageCmdObj = new ArrayList();
        DataTable dtaPageSet = dsePageInfo.Tables["PageSet"];
        DataTable dtaPage = dsePageInfo.Tables["Page"];
        DataTable dtaPageCtrl = dsePageInfo.Tables["PageCtrl"];
        DataTable dtaPageInputs;
        security objSec = new security();
        for (int i = 0; i < dtaPage.Rows.Count; i++)
        {
            if (!Convert.ToBoolean(objSec.getObjectRightsForUser(dtaPage.Rows[i]["object_id"].ToString(), "create"))) continue;
            strPageId = dtaPage.Rows[i]["page_id"].ToString();
            sAF_ROW_ID = dtaPage.Rows[i]["af_row_id"].ToString();
            dtaPageInputs = dseInputInfo.Tables[strPageId];
            arlPageCmdObj = InsertPage(dtaPage.Rows[i], dtaPageCtrl.Select("page_id='" + sAF_ROW_ID + "'"), dseInputInfo.Tables[strPageId]);
            for (int j = 0; j < arlPageCmdObj.Count; j++)
            {
                arlCmdObj.Add(arlPageCmdObj[j]);
            }
        }
        return arlCmdObj;
    }
    public string getDateValues(string strFldValue, bool convertToUniversal)
    {
        try
        {
            if (convertToUniversal)
                return DateTime.Parse(strFldValue, System.Globalization.CultureInfo.InvariantCulture).ToUniversalTime().ToString();
            if (HttpContext.Current.Request.QueryString["PK"] == null)
                return DateTime.Parse(strFldValue, System.Globalization.CultureInfo.InvariantCulture).ToUniversalTime().ToString();
            else
                return DateTime.Parse(strFldValue, System.Globalization.CultureInfo.InvariantCulture).ToString();
        }
        catch (Exception e)
        {
            return strFldValue;
        }
    }
    public ArrayList InsertPage(DataRow droPage, DataRow[] droPageCtrl, DataTable dtaInputInfo)
    {
        ArrayList arlReturn = new ArrayList();
        string strSql = "";
        string strFlds = "";
        string strVals = "";
        string strFldName = "";
        string strFldValue = "";
        string strDataType = "";
        string strTableName = droPage["page_table_name"].ToString();
        SqlCommand cmdObj;
        SqlParameter spmObj;
        for (int i = 0; i < dtaInputInfo.Rows.Count; i++)
        {
            strFlds = "";
            strVals = "";
            cmdObj = new SqlCommand();
            for (int j = 0; j < droPageCtrl.Length; j++)
            {
                spmObj = new SqlParameter();
                /////strFldName = droPageCtrl[j]["control_id"].ToString();
                /////j
                strFldName = droPageCtrl[j]["control_attrib_id"].ToString();
                strDataType = droPageCtrl[j]["control_attrib_type"].ToString();
                strFldValue = dtaInputInfo.Rows[i][strFldName].ToString();
                if (strFldName != "created_by" && strFldName != "created_at" && strFldName != "modified_at" && strFldName != "modified_by" && strFldName != "af_row_id" && strFldName != "owner_id")
                    if (!Convert.ToBoolean(droPageCtrl[j]["control_visible"].ToString()) && droPageCtrl[j]["default_value"].ToString() == "" && strFldValue == "")
                        continue;
                if (strFlds != "")
                {
                    strFlds += ",";
                }
                if (strVals != "")
                {
                    strVals += ",";
                }
                strFlds += " [" + strFldName + "]";
                strVals += "@" + strFldName.Replace("'", "").Replace(" ", "#").Replace(":","#");
                strFldName = "@" + strFldName.Replace(" ", "#").Replace("'", "").Replace(":", "#");

                switch (strDataType.ToUpper())
                {
                    case GlobalValues.Text:
                        if (strFldValue == "")
                            cmdObj.Parameters.Add(strFldName, SqlDbType.NVarChar ).Value = DBNull.Value;
                        else
                            cmdObj.Parameters.Add(strFldName, SqlDbType.NVarChar).Value = strFldValue;
                        break;
                    case GlobalValues.Number:
                        if (strFldValue == "")
                            cmdObj.Parameters.Add(strFldName, SqlDbType.Int).Value = DBNull.Value;
                        else
                            cmdObj.Parameters.Add(strFldName, SqlDbType.Int).Value = strFldValue;
                        break;
                    case GlobalValues.Date:
                        if (strFldValue == "")
                            cmdObj.Parameters.Add(strFldName, SqlDbType.DateTime).Value = DBNull.Value;
                        else
                            cmdObj.Parameters.Add(strFldName, SqlDbType.DateTime).Value = strFldValue;
                        break;
                    case GlobalValues.DateTime:
                        if (strFldValue == "")
                            cmdObj.Parameters.Add(strFldName, SqlDbType.DateTime).Value = DBNull.Value;
                        else
                        {
                            cmdObj.Parameters.Add(strFldName, SqlDbType.DateTime).Value = strFldValue;
                            //////DynamicDisplayI DDI = new DynamicDisplayI();
                            //////string sUTC_Offset = DDI.getUserSetting("sy_usr", "time_zone", "sy_timezone", "utc_offset", "timezone_id");
                            //////if (sUTC_Offset == "" | sUTC_Offset == "0")
                            //////{
                            //////    //if (HttpContext.Current.Request.QueryString["PK"] == null)
                            //////    //    cmdObj.Parameters.Add(strFldName, SqlDbType.DateTime).Value = DateTime.Parse(strFldValue, System.Globalization.CultureInfo.InvariantCulture).ToUniversalTime().ToString();
                            //////    //else
                            //////    //    cmdObj.Parameters.Add(strFldName, SqlDbType.DateTime).Value = DateTime.Parse(strFldValue, System.Globalization.CultureInfo.InvariantCulture).ToString();
                            //////    cmdObj.Parameters.Add(strFldName, SqlDbType.DateTime).Value = getDateValues(strFldValue,false);
                            //////}
                            //////else
                            //////{
                            //////    //cmdObj.Parameters.Add(strFldName, SqlDbType.DateTime).Value = DateTime.Parse(strFldValue, System.Globalization.CultureInfo.InvariantCulture).ToUniversalTime().ToString();
                            //////    cmdObj.Parameters.Add(strFldName, SqlDbType.DateTime).Value = getDateValues(strFldValue, true);
                            //////}
                            //////DDI = null;
                        }
                        break;
                    case GlobalValues.Boolean:
                        if (strFldValue != "")
                        {
                            if (strFldValue == "1")
                                strFldValue = "True";
                            else if (strFldValue == "0")
                                strFldValue = "False";
                        }
                        if (strFldValue == "")
                            cmdObj.Parameters.Add(strFldName, SqlDbType.Bit).Value = DBNull.Value;
                        else if (Convert.ToBoolean(strFldValue))
                            cmdObj.Parameters.Add(strFldName, SqlDbType.Bit).Value = 1;
                        else
                            cmdObj.Parameters.Add(strFldName, SqlDbType.Bit).Value = 0;
                        break;
                    case GlobalValues.Decimal:
                        if (strFldValue == "")
                            cmdObj.Parameters.Add(strFldName, SqlDbType.Decimal).Value = DBNull.Value;
                        else
                            cmdObj.Parameters.Add(strFldName, SqlDbType.Decimal).Value = strFldValue;
                        break;
                    case GlobalValues.Float:
                        if (strFldValue == "")
                            cmdObj.Parameters.Add(strFldName, SqlDbType.Float).Value = DBNull.Value;
                        else
                            cmdObj.Parameters.Add(strFldName, SqlDbType.Float).Value = strFldValue;
                        break;
                    default:
                        cmdObj.Parameters.Add(strFldName, SqlDbType.NVarChar).Value = strFldValue;
                        break;
                }
            }
            strSql = "insert into [" + strTableName + "] (" + strFlds + ") values (" + strVals + ")";
            cmdObj.CommandText = strSql;
            arlReturn.Add(cmdObj);
        }
        return arlReturn;
    }
}
